package com.kcwl.carrier.infrastructure.repository.persostence;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.QualificationInfo;
import com.kcwl.carrier.domain.repository.IQualificationInfoRepository;
import com.kcwl.carrier.infrastructure.repository.mapper.QualificationMapper;
import com.kcwl.carrier.infrastructure.repository.po.QualificationInfoPO;
import com.kcwl.carrier.interfaces.enums.VehicleInfoEnums;
import com.kcwl.carrier.interfaces.query.QualificationInfoQuery;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zxx
 * @date 2022-12-31
 */
@Slf4j
@Service
public class QualificationRepositoryImpl extends BaseRepositoryImpl<QualificationMapper, QualificationInfoPO> implements IQualificationInfoRepository {
    @Resource
    private QualificationMapper qualificationMapper;

    @Override
    public QualificationInfoPO getByBizId(QualificationInfo qualificationInfo) {
        QueryWrapper<QualificationInfoPO> wrapper = new QueryWrapper<>();
        wrapper.eq("biz_id", qualificationInfo.getBizId());
        wrapper.eq("biz_type", qualificationInfo.getBizType());
        return qualificationMapper.selectOne(wrapper);
    }

    @Override
    public KcPage<QualificationInfoPO> queryQualificationInfoPage(QualificationInfoQuery query) {
        QueryWrapper<QualificationInfoPO> wrapper = new QueryWrapper<>();
        //公共条件
        wrapper.eq(ObjectUtil.isNotEmpty(query.getId()), "id", query.getId());
        wrapper.eq(ObjectUtil.isNotEmpty(query.getBizType()), "biz_type", query.getBizType());
        wrapper.eq("authentication_status", VehicleInfoEnums.AuthenticationStatusEnum.UNVERIFIED.getCode());
        wrapper.in(ObjectUtil.isNotEmpty(query.getBizIds()),"biz_id",query.getBizIds());
        wrapper.ge(ObjectUtil.isNotEmpty(query.getSubmitTimeStart()), "submit_time", query.getSubmitTimeStart());
        wrapper.le(ObjectUtil.isNotEmpty(query.getSubmitTimeEnd()), "submit_time", query.getSubmitTimeEnd());
        //驾驶证相关条件
        wrapper.eq(StringUtil.isNotEmpty(query.getDrivingLicenceNo()), "driving_licence_no", query.getDrivingLicenceNo());
        wrapper.like(StringUtil.isNotEmpty(query.getAllowDrivingModel()), "allow_driving_model", query.getAllowDrivingModel());
        wrapper.eq("is_del", YesNoEnum.NAY.getValue());
        wrapper.ge(ObjectUtil.isNotEmpty(query.getBirthDateStart()), "birth_date", query.getBirthDateStart());
        wrapper.le(ObjectUtil.isNotEmpty(query.getBirthDateEnd()), "birth_date", query.getBirthDateEnd());
        wrapper.ge(ObjectUtil.isNotEmpty(query.getDriverLicenseEndTimeStart()), "driver_license_end_time", query.getDriverLicenseEndTimeStart());
        wrapper.le(ObjectUtil.isNotEmpty(query.getDriverLicenseEndTimeEnd()), "driver_license_end_time", query.getDriverLicenseEndTimeEnd());
        //道路运输证相关条件
        wrapper.eq(StringUtil.isNotEmpty(query.getPlateNumber()), "plate_number", query.getPlateNumber());
        wrapper.eq(StringUtil.isNotEmpty(query.getTrailerPlateNumber()), "plate_number", query.getTrailerPlateNumber());
        IPage<QualificationInfoPO> iPage = qualificationMapper.selectPage(new Page<>(query.getCurPagerNo(), query.getPageSize()), wrapper);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }
}
